<template>
  <div class="nav-search-box">
    <div class="nav-search">
      <div class="nav_search_form">
        <input
          type="text"
          v-model="keyword"
          placeholder="搜索"
          class="nav-search-keyword"
        />
        <div class="nav-search-btn">
          <button
            @click="handleClick"
            class="el-icon-search nav-search-submit"
          ></button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    open: {
      type: Boolean,
      default: true,
    },
  },
  mounted() {
    if (!this.open) {
      this.keyword = this.$route.query.keyword;
    }
  },
  data() {
    return {
      keyword: "",
    };
  },
  methods: {
    handleClick() {
      if (this.open) {
        // 新建窗口跳转到搜索页面
        let searchUrl = this.$router.resolve({
          name: "search",
          query: { keyword: this.keyword },
        });
        window.open(searchUrl.href, "_blank");
      } else {
        this.$emit("searchKeyword", this.keyword.trim());
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.nav-search-box {
  margin: 0 10px;
  width: 500px;
  transition: width 0.3s;
}

.nav-search {
  position: relative;
}

.nav_search_form {
  display: block;
  padding: 0 38px 0 16px;
  border-radius: 2px;
  background-color: #fff;
  border: 1px solid #e7e7e7;
}

.nav-search-keyword {
  overflow: hidden;
  width: 100%;
  height: 34px;
  border: none;
  box-shadow: none;
  background-color: transparent;
  color: black;
  font-size: 14px;
  line-height: 34px;
  outline: none;
}

.nav-search-btn {
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  padding: 0;
  width: 48px;
  height: 36px;
  border: none;
  border-radius: 2px;
  background: #e7e7e7;
  line-height: 26px;
}

.nav-search-submit {
  position: absolute;
  top: 8px;
  right: 16px;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: black;
  font-size: 16px;
  line-height: 20px;
  cursor: pointer;
  transition: all 0.2s;
}

.nav-search-submit:hover {
  appearance: button;
}
</style>